package com.whfc.wxmp.api.fim.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.DateUtil;
import com.whfc.fim.dto.*;
import com.whfc.fim.param.FimResultEditParam;
import com.whfc.fim.param.FimSearchParam;
import com.whfc.fim.service.FimResultService;
import com.whfc.wxmp.api.fim.service.WxFimResultService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.IntSummaryStatistics;
import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-09-09 18:17
 */
@Service
public class WxFimResultServiceImpl implements WxFimResultService {

    @DubboReference(interfaceClass = FimResultService.class, version = "1.0.0")
    private FimResultService fimResultService;

    @Override
    public PageData<FimResultDTO> list(Integer pageNum, Integer pageSize, Integer deptId, Integer state,Integer algCheckType, Date startTime, Date endTime) {
        return fimResultService.list(pageNum, pageSize, deptId, state,algCheckType, startTime,endTime);
    }

    @Override
    public FimResultDTO detail(Integer resultId) {
        return fimResultService.detail(resultId);
    }

    @Override
    public FimResultAnalysisDTO analysisRank(Integer deptId, Date date) {
        return fimResultService.analysisRank(deptId, date);
    }

    @Override
    public void handle(FimResultEditParam editParam) {
        fimResultService.handle(editParam);
    }

    @Override
    public ListData<FimResultRateContDTO> analysisByDate(Integer deptId, Date date) {
        Date startTime = DateUtil.getDateBegin(date);
        Date endTime = DateUtil.getDateEnd(date);
        List<FimResultRateContDTO> list = fimResultService.warnNumstatic(deptId, null, startTime, endTime);
        return new ListData<>(list);
    }
    @Override
    public FimWxResultRateContDTO analysisByDateWX(Integer deptId, Date startTime,Date endTime,Integer state) {

        if(null==state){
            List<FimResultRateContDTO> handleList = fimResultService.warnNumstatic(deptId, 1, startTime, endTime);
            List<FimResultRateContDTO> unHandleList = fimResultService.warnNumstatic(deptId, 0, startTime, endTime);

            List<FimResultRateContDTO> list = fimResultService.warnNumstatic(deptId, null, startTime, endTime);

            Integer handleUnm = handleList.stream().mapToInt(FimResultRateContDTO::getCount).sum();
            Integer unHandleUnm = unHandleList.stream().mapToInt(FimResultRateContDTO::getCount).sum();
            return FimWxResultRateContDTO.builder().detail(list).total(handleUnm+unHandleUnm).handleNum(handleUnm).unHandleNum(unHandleUnm).build();
        }else{
            List<FimResultRateContDTO> list = fimResultService.warnNumstatic(deptId, state, startTime, endTime);
            Integer total = list.stream().mapToInt(FimResultRateContDTO::getCount).sum();
            return FimWxResultRateContDTO.builder().detail(list).total(total).build();
        }



    }

    @Override
    public FimWarnAnalysisDTO analysisByMonth(Integer deptId, Date month) {
        Date monthBegin = DateUtil.getMonthBegin(month);
        Date monthEnd = DateUtil.getMonthEnd(month);
        FimWarnAnalysisDTO fimWarnAnalysisDTO = fimResultService.analysisByDate(deptId, monthBegin, monthEnd);
        return fimWarnAnalysisDTO;
    }

    @Override
    public PageData<FimResultDTO> monthDatalist(Integer pageNum, Integer pageSize, Integer deptId, Integer algCheckType, Integer handleStatus, Date month) {
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        PageData<FimResultDTO> pageData = fimResultService.list(pageNum, pageSize, deptId, handleStatus, algCheckType, startTime, endTime);
        return pageData;
    }

    @Override
    public FimWarnNumDTO getWarnNum(Integer deptId, Integer algCheckType, Date month) {
        Date startTime = DateUtil.getMonthBegin(month);
        Date endTime = DateUtil.getMonthEnd(month);
        FimWarnNumDTO warnNum = fimResultService.getWarnNum(deptId, algCheckType, startTime, endTime);
        return warnNum;
    }

    @Override
    public FimWarnStatisticsDetailDTO getFimWarnStatisticsDetail(FimSearchParam param) {
        return fimResultService.getFimWarnStatisticsDetail(param);
    }
}
